551B - ZgukistringZ - CodeForces Solution


brute force constructive algorithms implementation strings *1800

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <climits>

using namespace std;

int main() {
    int rec[3][26] = {0};
    int dup[26] = {0};
    string a, b, c;
    cin >> a >> b >> c;

    for (auto &n: a) {
        rec[0][n - 'a']++;
    }
    for (auto &n: b) {
        rec[1][n - 'a']++;
    }
    for (auto &n: c) {
        rec[2][n - 'a']++;
    }

    int l = 0, r = 0;

    int ans = 0;
    bool flag = true;
    for (int i = 0; flag; ++i) {
        int cur = INT_MAX;
        for (int j = 0; j < 26; ++j) {
            if (rec[2][j] != 0) {
                cur = min(cur, rec[0][j] / rec[2][j]);
            }
        }

        if (cur + i > ans) {
            ans = cur + i;
            l = i, r = cur;
            for (int j = 0; j < 26; ++j) {
                dup[j] = rec[0][j];
                dup[j] -= cur * rec[2][j];
            }
        }
        // cout << "for: " << cur << ", " << i << endl;


        for (int j = 0; j < 26; ++j) {
            rec[0][j] -= rec[1][j];
            if (rec[0][j] < 0) {
                flag = false;
                break;
                // goto L1;
            }
        }
    }
L1:  
    for (int i = 0; i < l; ++i) {
        // cout << b;
        printf("%s", b.c_str());
    }
    for (int i = 0; i < r; ++i) {
        // cout << c;
        printf("%s", c.c_str());
    }
    if (l + r == 0) {
        printf("%s", a.c_str());
    } else {
        for (int i = 0; i < 26; ++i) {
            for (int j = 0; j < dup[i]; ++j) {
                printf("%c", i + 'a');
                // cout << (char)(i + 'a');
            }
        }
    }
}


Comments

Submit
0 Comments
More Questions

1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold
1268. Search Suggestions System
841. Keys and Rooms
152. Maximum Product Subarray
337. House Robber III
869. Reordered Power of 2
1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer
1525. Number of Good Ways to Split a String
72. Edit Distance
563. Binary Tree Tilt
1306. Jump Game III
236. Lowest Common Ancestor of a Binary Tree
790. Domino and Tromino Tiling